#coding=utf-8
import unittest
import os, sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from api.base_api.login_base_api import LoginBaseApi
from api.base_api.car_apply import CarApply
from fixture import user

class CarApplyTest(LoginBaseApi):
    ''' 测试申请租车 '''

    def setUp(self):
        self.mobile = '13651020524'
        self.password = '123abc'
        self.car_apply_url = "http://192.168.1.141:8001/car-apply"
        self.car_brand = '宝马'
        self.car_price = '10万以到20万'

    def tearDown(self):
        user.clear_car_apply(self.mobile)

    def test_car_apply_success(self):
        ''' 申请租车成功 '''
        header = LoginBaseApi().login_base_api(self.mobile, self.password)
        payload =CarApply().car_apply(self.car_brand, self.car_price)
        response = self.post(self.car_apply_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 200)

        db_car_brand = user.select_car_apply(self.mobile)['car_brand']
        self.assertEqual(db_car_brand, self.car_brand)

        db_car_price = user.select_car_apply(self.mobile)['car_price']
        self.assertEqual(db_car_price, self.car_price)

    def test_car_apply_fail(self):
        '''未登录申请租车 '''
        payload = CarApply().car_apply(self.car_brand, self.car_price)
        response = self.post(self.car_apply_url, payload)
        status_code = response.status_code
        self.assertIsNot(status_code, 200)

if __name__ == '__main__':

    unittest.main()
